package com.cr.crsystem.system.service.impl;

import com.cr.crsystem.common.base.constant.CommonConstant;
import com.cr.crsystem.system.entity.Car;
import com.cr.crsystem.system.entity.Rent;
import com.cr.crsystem.system.entity.UserRent;
import com.cr.crsystem.system.mapper.RentMapper;
import com.cr.crsystem.system.service.CarService;
import com.cr.crsystem.system.service.RentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cr.crsystem.system.service.UserRentService;
import com.cr.crsystem.system.vo.RentCarVo;
import com.cr.crsystem.system.vo.RentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author su
 * @since 2022-06-21
 */
@Service
public class RentServiceImpl extends ServiceImpl<RentMapper, Rent> implements RentService {

    @Autowired
    private UserRentService userRentService;
    @Autowired
    private CarService carService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RentVo addRent(RentVo rentVo) {
        // 判断租借人是否和车主是同一人
        Car car = carService.getById(rentVo.getCarId());
        if(car.getUserId()==rentVo.getUserId()) return null;

        Rent rent = new Rent();
        BeanUtils.copyProperties(rentVo, rent);
        rent.setStatus(CommonConstant.RENT_1);
        save(rent);
        //设置UserRent
        UserRent userRent = new UserRent();
        userRent.setUserId(rentVo.getUserId());
        userRent.setRentId(rent.getId());
        userRentService.save(userRent);
        //修改Car
        car.setIsRent(CommonConstant.REND);
        carService.updateById(car);
        rentVo.setId(rent.getId());
        return rentVo;
    }

    @Override
    public List<RentCarVo> getPersonRent(Integer userId) {
        List<RentCarVo> personRent = baseMapper.getPersonRent(userId);
        return personRent;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<RentCarVo> getPersonOrder(Integer userId) {
        List<RentCarVo> personOrder = baseMapper.getPersonOrder(userId);
        return personOrder;
    }

    @Override
    public boolean cancel(RentVo rentVo) {
        Car car = carService.getById(rentVo.getCarId());
        car.setIsRent(CommonConstant.NOT_REND);
        carService.updateById(car);
        Rent rent = new Rent();
        BeanUtils.copyProperties(rentVo, rent);
        rent.setStatus(CommonConstant.RENT_3);
        baseMapper.updateById(rent);
        return true;
    }

    @Override
    public boolean finish(RentVo rentVo) {
        Car car = carService.getById(rentVo.getCarId());
        car.setIsRent(CommonConstant.NOT_REND);
        carService.updateById(car);
        Rent rent = new Rent();
        BeanUtils.copyProperties(rentVo, rent);
        rent.setStatus(CommonConstant.RENT_2);
        baseMapper.updateById(rent);
        return false;
    }
}
